package cn.sc.summer.construction.route;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.sc.summer.constant.util.AssertUtil;
import cn.sc.summer.domain.route.SysApiService;
import cn.sc.summer.exception.enums.CommonExceptionEnum;
import cn.sc.summer.mybatis.annotation.TenantIgnore;
import cn.sc.summer.mybatis.mapper.SysApiMapper;
import cn.sc.summer.mybatis.po.SysApi;
import cn.sc.summer.protocol.params.SysApiInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：api接口实现层
 *
 * @author a-xin
 * @date 2024/5/23 17:09
 */
@Slf4j
@Service
public class SysApiServiceImpl implements SysApiService {

    @Resource
    private SysApiMapper sysApiMapper;

    /**
     * 校验API是否合法
     *
     * @param apiIds api接口ID
     */
    @Override
    @TenantIgnore
    public void checkApi(List<String> apiIds) {
        AssertUtil.functionIfPre(apiIds, CollUtil::isNotEmpty, () -> {
            int size = sysApiMapper.selectBatchIds(apiIds).size();
            CommonExceptionEnum.NORMAL_EXCEPTION.exceptionIf(apiIds.size() != size, "不存在对应的API接口信息!");
        });
    }

    /**
     * 获取所有api接口信息
     *
     * @return api接口信息
     */
    @Override
    @TenantIgnore
    public List<SysApiInfo> getAllApiInfo() {
        return sysApiMapper.selectList()
                .stream().map(item -> BeanUtil.toBean(item, SysApiInfo.class))
                .collect(Collectors.toList());
    }

    /**
     * 获取api接口信息
     *
     * @param apiIds api接口ID
     * @return api接口信息
     */
    @Override
    @TenantIgnore
    public List<SysApiInfo> getApiInfo(List<String> apiIds) {
        return AssertUtil.functionIfPre(apiIds, CollUtil::isNotEmpty, () -> sysApiMapper.selectList(new LambdaQueryWrapper<SysApi>()
                        .in(SysApi::getApi, apiIds))
                .stream().map(item -> BeanUtil.toBean(item, SysApiInfo.class))
                .collect(Collectors.toList()), new ArrayList<>());
    }

}
